package com.yumeng.system.main.auth;

import com.yumeng.common.auth.BaseAuthInfo;
import com.yumeng.common.data.constants.FlagConstants;
import com.yumeng.common.data.enums.AuthMode;
import com.yumeng.common.exception.YumengException;
import com.yumeng.framework.auth.bean.AuthExtDataInfo;
import com.yumeng.framework.auth.bean.authinfo.NormalAuthInfo;
import com.yumeng.framework.auth.data.constants.AuthConstants;
import com.yumeng.framework.auth.service.AuthExtDataHandler;
import com.yumeng.framework.auth.service.auth.BaseAuthService;
import com.yumeng.framework.data.enums.LockedFlag;
import com.yumeng.framework.data.enums.Status2Flag;
import com.yumeng.system.main.dao.SysUserDao;
import com.yumeng.system.main.entity.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

import java.util.HashSet;
import java.util.Set;

/**
 * @author wxd
 * @date 2025/4/10 10:45
 */
public abstract class BaseAuthServiceImpl implements BaseAuthService {

    protected abstract BaseAuthInfo doFindByAuthKey(String authKey);

    protected SysUserDao sysUserDao;
    @Autowired
    public void setSysUserMapper(SysUserDao sysUserDao) {
        this.sysUserDao = sysUserDao;
    }

    private AuthExtDataHandler authExtDataHandler;
    @Lazy //securityConfig normalAuthServiceImpl循环依赖
    @Autowired(required = false)
    public void setAuthExtDataHandler(AuthExtDataHandler authExtDataHandler) {
        this.authExtDataHandler = authExtDataHandler;
    }


    @Override
    public BaseAuthInfo findByAuthKey(String authKey) {
        return doFindByAuthKey(authKey);
    }

    @Override
    public AuthExtDataInfo getAuthExtDataInfo(BaseAuthInfo authInfo) {
        if (authExtDataHandler != null) {
            return authExtDataHandler.getAuthExtDataInfo(authInfo);
        }
        return null;
    }

    protected BaseAuthInfo createAuthInfo(SysUser sysUser, AuthMode authMode) {
        NormalAuthInfo authInfo = new NormalAuthInfo();
        authInfo.setIdentityId(sysUser.getUserId().toString());
        authInfo.setIdentityName(StringUtils.isNotBlank(sysUser.getNickname()) ? sysUser.getNickname() : sysUser.getRealName());
        authInfo.setAuthMode(authMode);
        if (AuthMode.NORMAL.equals(authMode)) {// 待添加 || AuthMode.FACE.equals(authMode)
            authInfo.setAuthKey(sysUser.getUsername());
        } else if (AuthMode.SMS.equals(authMode)) {
            authInfo.setAuthKey(sysUser.getPhone());
        } else {
            throw new YumengException("认证方式未识别：%s", authMode);
        }
        authInfo.setCredentials(sysUser.getPassword());
        authInfo.setSalt(sysUser.getSalt());
        authInfo.setUserTypes(sysUser.getUserTypes());
        authInfo.setEnabled(Status2Flag.ENABLE.equals(sysUser.getStatus()));
        authInfo.setAccountLocked(!LockedFlag.UN_LOCKED.equals(sysUser.getLockFlag()));
        authInfo.setAccountExpired(false);
        authInfo.setCredentialsExpired(false);

        Set<String> authorities = new HashSet<>();
        if (StringUtils.isNotBlank(sysUser.getRoles())){
            String[] roles = sysUser.getRoles().split(FlagConstants.COMMA_FLAG);
            for (String role : roles) {
                if (StringUtils.isNotBlank(role)) {
                    if (role.startsWith(AuthConstants.ROEL_PREFIX)){
                        authorities.add(role);
                    }else {
                        authorities.add(AuthConstants.ROEL_PREFIX + role);
                    }
                }
            }
        }
        if (StringUtils.isNotBlank(sysUser.getPerms())){
            String[] perms = sysUser.getPerms().split(FlagConstants.COMMA_FLAG);
            for (String perm : perms) {
                if (StringUtils.isNotBlank(perm)) {
                    authorities.add(perm);
                }
            }
        }
        authInfo.setAuthorities(authorities);
        return authInfo;
    }
}
